package cn.cknight.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

/**
 * 自动装配 自动配置入口
 * Author: cKnight
 * Date: 2024/4/12
 */
@AutoConfiguration
//类路径需要存在SmsService才能被spring装载
@ConditionalOnClass(SmsService.class)
//@EnableConfigurationProperties负责导入这个SmsProperties已经绑定了属性的bean到spring容器中
@EnableConfigurationProperties(value = SmsProperties.class)
public class SmsAutoConfiguration {

    @Autowired
    private SmsProperties smsProperties;

    //创建SmsService bean
    //当bean不存在时 就创建此ben
    @ConditionalOnMissingBean
    @Bean
    public SmsService smsService(){
        System.out.println("===========默认smsBean启动===========");
        return (phone, msg) -> {
            System.out.println("采用默认sms短信服务发送短信"+smsProperties.getTempleId());
        };
    }

}
